class MyClass {
    name;
    age = 1;
}

var myclass = new MyClass()
console.log(myclass); // {name:undefined,age:1}

// 类的私有方法和私有属性
class Person {
    name = "SunWuKong";
    #age = 18; // 当前变量智能在类的内部进行访问
    sayAge() {
        this.#sayAge()
    }
    #sayAge() {
        console.log(this.#age);
    }
}

var person = new Person()
person.sayAge() // 18

class Dog {
    static name = "旺财"
    static sayName() {
        console.log(`我是小狗${this.name}`);
    }
}

console.log(Dog.sayName()); // 我是小狗旺财

var dog = new Dog()
// console.log(dog.sayName()); 报错 dog.sayName is not a function

// 静态块
// class Translator {
//     static translations = {
//         yes: 'ja',
//         no: 'nein',
//         maybe: 'vielleicht',
//     };
//     static englishWords = extractEnglish(this.translations);
//     static germanWords = extractGerman(this.translations);
// }

// function extractEnglish(translations) {
//     return Object.keys(translations);
// }

// function extractGerman(translations) {
//     return Object.values(translations);
// }

// console.log(Translator.englishWords); // ['yes', 'no', 'maybe']
// console.log(Translator.germanWords); // ['ja', 'nein', 'vielleicht']


class Translator {
    static translations = {
        yes: 'ja',
        no: 'nein',
        maybe: 'vielleicht',
    };
    // 定义属性
    static englishWords = []
    static germanWords = []
    // 静态块(初始化的时候，能读到内部的属性值,做一些逻辑操作)
    static {
        for (const [englist, german] of Object.entries(this.translations)) {
            this.englishWords.push(englist)
            this.germanWords.push(german)
        }
    }
}

console.log(Translator.englishWords); // ['yes', 'no', 'maybe']
console.log(Translator.germanWords); // ['ja', 'nein', 'vielleicht']

